package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.PersonalActivityBean;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.beiing.tianshuai.tianshuai.widget.FooterView;
import com.beiing.tianshuai.tianshuai.widget.RoundAngleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.text.MessageFormat;
import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/7/28 0028.
 */

public class PersonalActivityAdapter extends BaseRecyclerViewAdapter<PersonalActivityBean.DataBean.NumberBean> {

    private static final int DYNAMIC_NORMAL = 0;                 // 普通动态
    private static final int ITEM_VIEW_TYPE_FOOTER = 2;          // 转发动态
    private Context mContext;
    private int mFooterStatus;

    public PersonalActivityAdapter(Context context) {
        this.mContext = context;
    }

    public void setFooterStatus(int footerStatus) {
        mFooterStatus = footerStatus;
    }

    @Override
    public int getItemViewType(int position) {
        int result;
        if (position == mList.size()) {
            result = ITEM_VIEW_TYPE_FOOTER;
        } else {
            result = DYNAMIC_NORMAL;
        }
        return result;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case DYNAMIC_NORMAL:
                holder = new ViewHolder(View.inflate(mContext, R.layout.item_personal_activity, null));
                break;
            case ITEM_VIEW_TYPE_FOOTER:
                if (mFooterView == null) {
                    mFooterView = new FooterView(mContext);
                    mFooterView.setLayoutParams(
                            new LinearLayout.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT
                            )
                    );
                }
                return new FooterViewHolder(mFooterView);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            PersonalActivityBean.DataBean.NumberBean bean = mList.get(position);
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(690, 390);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(
                            bean.getPhoto()) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.ACTIVITY_PIC_HEAD + bean.getCover())
                    .apply(requestOptions)
                    .into(((ViewHolder) holder).mIvActivityCover);
            ((ViewHolder) holder).mTxtActivityTitle.setText(bean.getStrech());
            ((ViewHolder) holder).mTxtActivityLocation.setText(bean.getLocation());
            try {
                String timeEnd = TimeUtils.longToString(Long.parseLong(bean.getExitTime()), "yyyy-MM-dd HH:mm");
                ((ViewHolder) holder).mTxtActivityDate.setText(
                        MessageFormat.format("{0}",
                                TimeUtils.getTimeDifference(
                                        TimeUtils.getNowTime()
                                        ,timeEnd
                                )
                        )
                );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (holder instanceof FooterViewHolder) {
            if (position == mList.size() && mFooterView.getStatus() != FooterView.NO_MORE) {
                setFooterViewStatus(mFooterStatus);
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_activity_cover)
        RoundAngleImageView mIvActivityCover;
        @BindView(R.id.txt_activity_title)
        TextView mTxtActivityTitle;
        @BindView(R.id.txt_activity_location)
        TextView mTxtActivityLocation;
        @BindView(R.id.txt_activity_date)
        TextView mTxtActivityDate;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null && !mList.isEmpty()) {
            return mList.size();
        } else {
            return 0;
        }
    }
}
